package cn.ihelloworld.util;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializerProvider;
import org.codehaus.jackson.map.ser.CustomSerializerFactory;
import org.springframework.stereotype.Component;

/**
 *
 * @author zc
 * @creation 2013-9-28
 * TODO
 */
@Component
public class CustomObjectMapper extends ObjectMapper
{
  public CustomObjectMapper(){ 
    CustomSerializerFactory factory = new CustomSerializerFactory(); 
    factory.addGenericMapping(java.util.Date.class, new JsonSerializer<java.util.Date>(){

      @Override
      public void serialize(Date date, JsonGenerator generator, SerializerProvider provider)
           throws IOException,JsonProcessingException
      {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
        generator.writeString(sdf.format(date)); 
      } 
    }); 
   
    factory.addGenericMapping(java.sql.Date.class, new JsonSerializer<java.util.Date>(){

      @Override
      public void serialize(Date date, JsonGenerator generator, SerializerProvider provider)
           throws IOException,JsonProcessingException
      {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
        generator.writeString(sdf.format(date)); 
      } 
    }); 
   
    this.setSerializerFactory(factory); 
} 
}
